package com.flycent.fanbao.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.Volley;
import com.flycent.fanbao.App;
import com.flycent.fanbao.Constants;
import com.flycent.fanbao.R;
import com.flycent.fanbao.adapter.AbsViewHolderAdapter;
import com.flycent.fanbao.bean.Express;
import com.flycent.fanbao.bean.ExpressAll;
import com.flycent.fanbao.bean.UserDao;
import com.flycent.fanbao.net.RequestRoute;
import com.flycent.fanbao.net.request.JsonRequestWithAuth;
import com.flycent.fanbao.ui.view.CircularLoadingView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.volley.easy.Response.CallBackListener;
import com.volley.easy.VolleyError;

//http://api.36wu.com/Express/GetExpressInfo?postid=762676547236&format=json&authkey=49dc6c6794344460a4c6cdcc0ffe875f

public class ExpressInfoActivity extends BackableTitleBarActivity {
	@ViewInject(id = R.id.order_track_list) ListView mList;
	@ViewInject(id = R.id.circularLoadindView) CircularLoadingView mCircularLoadingView;

	public static String AuthKey = "49dc6c6794344460a4c6cdcc0ffe875f";
	private ExpressAdapter mAdapter;
	private List<Express> mExpresses = new ArrayList<Express>();
	public static String ssString = "http://182.254.131.222/Express/GetExpressInfo?postid=762676547236&format=json&authkey=49dc6c6794344460a4c6cdcc0ffe875f";
	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_expressinfo);	
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle("快递详情");

		mAdapter = new ExpressAdapter(this, R.layout.item_express_detail);
		mList.setAdapter(mAdapter);

		//				getExpressInfoDetail();

		getExpressInfoDetail1();

		//		volleyRequest();


	}

	private void volleyRequest() {
		UserDao.getInstance().getExpressInfo(new CallBackListener<ExpressAll>() {

			@Override
			public void callBack(ExpressAll data) {
				ExpressAll expressAll = (ExpressAll) data;

				mAdapter.update(expressAll.getData());
				mCircularLoadingView.setVisibility(View.GONE);
			}

			@Override
			public void error(VolleyError error) {
				mCircularLoadingView.setVisibility(View.GONE);
			}

			@Override
			public void LogString(String result) {
				Log.d(TAG, result.toString());
			}


		});


	}

	private void getExpressInfoDetail1() {

		RequestQueue requestQueue = Volley.newRequestQueue(this);

		Map<String, String> appendHeader = new HashMap<String, String>();  
		appendHeader.put("username", "admin");  
		appendHeader.put("password", "123");  

		String uri = new RequestRoute().getExpressStr("762676547236", "json", "49dc6c6794344460a4c6cdcc0ffe875f");
		JsonRequestWithAuth<ExpressAll> expressRequest = 
				new JsonRequestWithAuth<ExpressAll>(uri, ExpressAll.class, 
						new Listener<ExpressAll>() {

					@Override
					public void onResponse(ExpressAll response) {
						App.showLog("response=="+response);

						mAdapter.update(response.getData());
						mCircularLoadingView.setVisibility(View.GONE);
					}

				}, appendHeader, null);

		requestQueue.add(expressRequest);
		requestQueue.start();
	}

	private void getExpressInfoDetail(){
		//		showLoadingDialog("");

		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("postid", "762676547236");//762676547236
		params.put("format", "json");
		params.put("authkey", AuthKey);

		fh.get(Constants.ExpressInfoIP,params, new AjaxCallBack<Object>() {

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				//				dismissLoadingDialog();
				mCircularLoadingView.setVisibility(View.GONE);
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				//				dismissLoadingDialog();
				mCircularLoadingView.setVisibility(View.GONE);
				try {

					JSONObject jsonObject = new JSONObject(t.toString());
					Gson gson = new Gson();
					mExpresses = gson.fromJson(jsonObject.getJSONArray("data").toString(),
							new TypeToken<List<Express>>(){}.getType());

					mAdapter.update(mExpresses);

				} catch (Exception e) {
				}

			}

		});

	}

	public class ExpressAdapter extends AbsViewHolderAdapter<Express>{

		public ExpressAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, Express itemData) {

			TextView expresstime = getViewFromHolder(R.id.expresstime);
			TextView expressdatails = getViewFromHolder(R.id.expressdatails);
			View mAbovelin = getViewFromHolder(R.id.above_lin);
			View mButtomlin = getViewFromHolder(R.id.buttom_lin);
			ImageView mImg = getViewFromHolder(R.id.order_left_point_img);

			expresstime.setText(itemData.getAcceptTime());
			expressdatails.setText(itemData.getRemark());

			if(pos == 0 ){
				mImg.setImageResource(R.drawable.order_track_arrive);
				mButtomlin.setVisibility(View.VISIBLE);
				mAbovelin.setVisibility(View.GONE);
			}else{
				mImg.setImageResource(R.drawable.order_track_point);
				if(pos == (mAdapter.getData().size()-1)){//
					mButtomlin.setVisibility(View.GONE);
					mAbovelin.setVisibility(View.VISIBLE);
				}else {
					mAbovelin.setVisibility(View.VISIBLE);
					mButtomlin.setVisibility(View.VISIBLE);
				}
			}

		}

	}


}
